package com.capgemini.gae.data;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;

import com.google.appengine.api.datastore.Key;

@Entity
public class Athlete extends Person {

	private int athleteNumber;

	@Basic
	private Set<Key> trainers;

	@Basic
	private Set<Key> performances = new HashSet<Key>();

	@Basic
	private List<String> dopingHistory;

	@Enumerated(EnumType.STRING)
	private Discipline sport;

	public Athlete(String name, String surName, Date birthDate,
			String nationality, Discipline sport, int number) {
		super(name, surName, birthDate, nationality);
		this.sport = sport;
		this.athleteNumber = number;
		this.dopingHistory = new ArrayList<String>();
		this.trainers = new HashSet<Key>();
		this.performances = new HashSet<Key>();
	}

	public Athlete() {
	}

	public int getAthleteNumber() {
		return athleteNumber;
	}

	public void setAthleteNumber(int athleteNumber) {
		this.athleteNumber = athleteNumber;
	}

	public List<String> getDopingHistory() {
		return dopingHistory;
	}

	public void addDopingHistory(String doping) {
		this.dopingHistory.size();
		this.dopingHistory.add(doping);
	}

	public Set<Key> getTrainers() {
		this.trainers.size();
		return trainers;
	}

	public void addTrainer(Trainer trainer) {
		this.trainers.size();
		this.trainers.add(trainer.getKey());
	}

	public Set<Key> getPerformances() {
		this.performances.size();
		return performances;
	}

	public void addPerformance(Performance performance) {
		if (this.performances == null) {

			this.performances = new HashSet<Key>();
		}
		this.performances.size();
		this.performances.add(performance.getKey());
	}

	public Discipline getSport() {
		return sport;
	}

	public void setSport(Discipline sport) {
		this.sport = sport;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + athleteNumber;
		result = prime * result
				+ ((dopingHistory == null) ? 0 : dopingHistory.hashCode());

		result = prime * result
				+ ((performances == null) ? 0 : performances.hashCode());

		result = prime * result + ((sport == null) ? 0 : sport.hashCode());

		result = prime * result
				+ ((trainers == null) ? 0 : trainers.hashCode());

		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Athlete other = (Athlete) obj;
		if (athleteNumber != other.athleteNumber)
			return false;
		if (dopingHistory == null) {
			if (other.dopingHistory != null)
				return false;
		} else if (!dopingHistory.equals(other.dopingHistory))
			return false;

		if (performances == null) {
			if (other.performances != null)
				return false;
		} else if (!performances.equals(other.performances))
			return false;

		if (sport != other.sport)
			return false;
		if (trainers == null) {
			if (other.trainers != null)
				return false;
		} else if (!trainers.equals(other.trainers))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Athlete [athleteNumber=" + athleteNumber + ", trainers="
				+ trainers + ", performances=" + performances
				+ ", dopingHistory=" + dopingHistory + ", sport=" + sport
				+ ", Adress= " + this.getAdress() + "]";
	}

	@Override
	public Class getEntityClass() {
		return Athlete.class;
	}

}
